// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    current: '1',
    recommendList:[],
    scrollHeight:0,
    offset:6,
    isTriggered:false,
  },
  bigImageClick(e){
    let id=e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + id
    });
  },
  handleRefresher(){
    this.setData({
      isTriggered:true
    })
    this.data.recommendList = Array.from(this.data.recommendList)      
    this.data.recommendList.sort(function(){
        return Math.random()-0.5
    })
    this.setData({
      isTriggered:false,
      recommendList:this.data.recommendList
    })    
  },
  handleChange({ detail }){
    this.setData({
      current: detail.key
    });
  },
  getData(limit,offset){
    wx.request({
      url: app.globalData.server_url+'getVideo',
      data: {
        limit:limit,
        offset:offset
      },
      header: {
        'content-type': 'application/json' // 默认值
      //  "Content-Type": "application/x-www-form-urlencoded" //用于post
      },
      method: 'GET',
      success: (res)=>{
        if(res.data.data){
          this.data.offset=this.data.offset+4
          if(this.data.recommendList.length==0){
            this.setData({
              recommendList: res.data.data
            });          
          }else{
            this.data.recommendList.push(...res.data.data)
            this.setData({
              recommendList:this.data.recommendList
            })
          }  
        }
      },
      fail: function (res) {
        console.log(res);
       },
    })
  },
  addList(){
    this.getData(4,this.data.offset)
  },
  onLoad(){
    this.getData(6,0)
  },
  onReady() {
    // 设置 scroll-view 高度，才能进行滚动
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          scrollHeight: res.screenHeight * res.pixelRatio,
        });
      }
    });
  },
})
